/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.dao;

import org.ipti.br.tag.entity.Zipcode;
import org.ipti.br.tag.entity.Zipcode_;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.ipti.br.tag.util.EncryptionUtil;

/**
 *
 * @author israel
 */
@Stateless
public class ZipcodeFacade extends AbstractFacade<Zipcode> {

    @PersistenceContext(unitName = "TAG2PU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public ZipcodeFacade() {
        super(Zipcode.class);
    }

    @Override
    public void create(Zipcode zipcode) {
        getEntityManager().persist(zipcode);
    }

    public List<Zipcode> findAllOrder() {
        return findAllOrderBy(Zipcode_.code);
    }

    public Zipcode searchByCode(int code) {
        try {
            CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
            CriteriaQuery<Zipcode> cq = cb.createQuery(Zipcode.class);
            Root<Zipcode> rt = cq.from(Zipcode.class);
            cq.where(cb.equal(rt.get(Zipcode_.code), code));
            return getEntityManager().createQuery(cq).getSingleResult();
        } catch(Exception e) {
            return null;
        }
    }
}
